package JDK常用类库.lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author zx
 * @create 2023-07-30 21:38
 * Function()函数如何用，Stream API如何使用，以及复杂Lambda的使用还需要多多练习
 */
public class LambdaTest {
    @Test
    public void test(){
        Comparator<Integer> comparator1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
        int res1 = comparator1.compare(10,23);
        System.out.println(res1);
        //Lambda表达式写法
        Comparator<Integer> comparator2 = (o1, o2) -> Integer.compare(o1,o2);
        int res2 = comparator2.compare(12,23);
        System.out.println(res2);
        //方法引用
        Comparator<Integer> comparator3 = Integer::compare;
        int res3 = comparator3.compare(10,16);
        System.out.println(res3);
    }

    /**
     * 接口实现 vs Lambda表达式
     */
    @Test
    public void test1(){
        List<String> list1 = Arrays.asList("John", "Alice", "Bob", "David");
        System.out.println(list1);
        Comparator<String> comparator1 = new Comparator<String>() {
            @Override
            public int compare(String name1, String name2) {
                return name1.length() - name2.length();
            }
        };
        list1.sort(comparator1);
        System.out.println(list1);

        List<String> list2 = Arrays.asList("John", "Alice", "Bob", "David");
        System.out.println(list2);
        Comparator<String> comparator2 = (name1, name2) -> name1.length() - name2.length();
        list2.sort(comparator2);
        System.out.println(list2);
    }

    /**
     * 接口实现 vs Lambda表达式 - 根据字符串长度进行升序排序(自己常用的写法)
     */
    @Test
    public void test2(){
        List<String> list1 = Arrays.asList("John", "Alice", "Bob", "David");
        System.out.println(list1);
        list1.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length() - o2.length();
            }
        });
        System.out.println(list1);

        List<String> list2 = Arrays.asList("John", "Alice", "Bob", "David");
        System.out.println(list2);
        list2.sort(Comparator.comparing(String::length));
        System.out.println(list2);
    }

    /**
     * 接口实现 vs Lambda表达式(常用,先按长度大小,再按照字典序进行排序)
     */
    @Test
    public void test3(){
        List<String> list1 = Arrays.asList("John", "Xiao", "Han", "Alice", "Bob", "David", "Russ");
        System.out.println(list1);
        list1.sort(new Comparator<String>() {
            @Override
            public int compare(String name1, String name2) {
                if (name1.length() == name2.length()) {
                    return name1.compareTo(name2);
                } else {
                    return name1.length() - name2.length();
                }
            }
        });
        System.out.println(list1);

        List<String> list2 = Arrays.asList("John", "Xiao", "Han", "Alice", "Bob", "David", "Russ");
        System.out.println(list2);
        list2.sort((name1, name2) -> {
            if (name1.length() == name2.length()) {
                return name1.compareTo(name2);
            } else {
                return name1.length() - name2.length();
            }
        });
        System.out.println(list2);
    }

    /**
     * 匿名内部类 vs Lambda表达式
     */
    @Test
    public void test4(){
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("西安市未央区");
            }
        });
        t1.start();
        Thread t2 = new Thread(() -> System.out.println("南京市江宁区"));
        t2.start();
    }

    /**
     * 线程处理 vs 使用CompletableFuture进行异步处理（这块不太熟）！！！
     */
    @Test
    public void test5(){
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("adfadfasdsfa");
            }
        });
        t1.start();
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("dafasdfa");
            }
        });
        Thread t2 = new Thread(() -> {
            // 异步操作
            System.out.println("Async operation");
        });
        t2.start();
        CompletableFuture.runAsync(() -> {
            // 异步操作
            System.out.println("Async operation");
        });
    }

    /**
     * 使用函数式接口
     */
    @Test
    public void test6(){
        LambdaTest.Calculator add1 = new LambdaTest.Calculator() {
            @Override
            public int calculate(int a, int b) {
                return a + b;
            }
        };
        LambdaTest.Calculator sub1 = new LambdaTest.Calculator() {
            @Override
            public int calculate(int a, int b) {
                return a - b;
            }
        };
        System.out.println(add1.calculate(10,23));
        System.out.println(sub1.calculate(10,23));

        LambdaTest.Calculator add2 = (a, b) -> a + b;
        LambdaTest.Calculator sub2 = (a, b) -> a - b;
        System.out.println(add2.calculate(10,16));
        System.out.println(sub2.calculate(10,16));
    }
    interface Calculator {
        int calculate(int a, int b);
    }
}
